package yxy.game.pm2.active.command.challenge;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actchallenge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.handle.Act_TimeChallengeHandle;
import yxy.game.pm2.active.module.ActiveModule_限时挑战;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.FighterModule;

import java.util.ArrayList;
import java.util.Collection;

@MessageType(proto = Command.ActChallengeInfo, classtype = ActiveMessage.ActChallengeInfoRequest.class)
public class CMD_Active_限时挑战_Info extends Command<ActiveMessage.ActChallengeInfoRequest> {

    private static final Logger logger = Logger.create(CMD_Active_限时挑战_Info.class);

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActChallengeInfoRequest request) throws Exception {
        String pcode = messageSource.getTag();

        ActiveMessage.ActChallengeInfoResponse.Builder response = ActiveMessage.ActChallengeInfoResponse.newBuilder();

        Act_TimeChallengeHandle handle = new Act_TimeChallengeHandle();

        ActiveModule_限时挑战 am = ActiveModule_限时挑战.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        response.setLayer(am.getLayer());

        for (int id : am.getIds()) {
            ActiveMessage.TimeChallengeInfo.Builder build = ActiveMessage.TimeChallengeInfo.newBuilder();

            int canget = am.getGetrewardids().contains(id) ? DrawStatus.hadget.getStatus() :
                    am.getVictoryids().contains(id) ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();

            actchallenge execl = ActDataCenter.getData(id, actchallenge.class, pcode);
            Collection<Currency> curs = Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
            Collection<PackageMessage.Item> iteams = new CurrencyHandle().packItems(curs);

            long power = 0;
            ArrayList<FighterModule> fighterModuleList = handle.getFighterModuleList(id);
            for (FighterModule fighterModule : fighterModuleList) {
                power += fighterModule.getTotalPower();
            }

            build.setPower(power);
            build.setId(id);
            build.setVictory(am.getVictoryids().contains(id) ? true : false);
            build.setCanget(canget);
            build.addAllCurs(iteams);

            response.addTcs(build.build());
        }

        send(messageSource, response, Result.Success);
    }


    private void send(MessageSource messageSource, ActiveMessage.ActChallengeInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
